package com.polaris.live.adapter.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.adapter.home.HomeRecommendAdapter.PlayViewHolder
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.PagConst.FREE_CARD_PAG
import com.polaris.live.common.constant.PagConst.HOME_FREE_CALL_PAG
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.dp
import com.polaris.live.databinding.AdapterHomeRecommendBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.normal
import com.polaris.live.utils.image.transformRounded
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.utils.zego.listener.OnLivePlayViewAlphaImpl

/**
 * HomeRecommendAdapter
 *
 * @author Created by lucas on 2023/10/17 16:29
 */
class HomeRecommendAdapter(
    private val firstPlayTopStream: FirstPlayTopStream,
) : BaseQuickAdapter<DisplayUserBean, PlayViewHolder>(R.layout.adapter_home_recommend) {

    private var firstLoadHolder = true

    init {
        addChildClickViewIds(R.id.bg, R.id.bg_call, R.id.video)
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): PlayViewHolder {
        return PlayViewHolder(
            AdapterHomeRecommendBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun convert(holder: PlayViewHolder, item: DisplayUserBean) {
        holder.binding.apply {
            Glide.with(root.context)
                .load(ImageUtils.imgPath(item.cover))
                .normal()
                .transformRounded(8.dp)
                .into(bg)

            country.setCountry(item.base.country)

            llLanguage.setLanguage(item.base.language, 18.autoDp, 18.autoDp, 10)

            svState.setState(item.base, item, 14f)

            nickname.text = item.base.nickname

            if (CommonUtils.isMan(UserManager.getUser().sex) && item.base.callPrice != null) {
                tvCallPrice.text = "${item.base.callPrice}/min"
                llCallPrice.visibility = View.VISIBLE
            } else {
                llCallPrice.visibility = View.GONE
            }

            pagView.apply {
                path = HOME_FREE_CALL_PAG
                setRepeatCount(-1)
                play()
            }

            //免费
            if (item.actualPrice == 0L) {
                call.setBackgroundResource(R.drawable.btn_call_free)
                callIcon.visibility = View.VISIBLE
                callIcon.apply {
                    visibility = View.VISIBLE
                    path = FREE_CARD_PAG
                    setRepeatCount(-1)
                    play()
                }
            } else {
                call.setBackgroundResource(R.drawable.btn_call_pay)
                callIcon.visibility = View.GONE
            }

            val position = holder.layoutPosition
            if (firstPlayTopStream.getCurrentPosition() != position) {
                videoCard.visibility = View.GONE
            }

            if (holder.layoutPosition == 0 && firstLoadHolder) {
                firstPlayTopStream.player(holder, item)
                firstLoadHolder = false
            }

            if (UserStateUtils.callUserState(item.base) == AppConst.YES) {
                bgCall.visibility = View.VISIBLE
            } else {
                bgCall.visibility = View.GONE
            }
        }
    }

    fun resetData(list: List<DisplayUserBean>) {
        firstLoadHolder = true
        setList(list)
    }

    class PlayViewHolder(val binding: AdapterHomeRecommendBinding) : BaseViewHolder(binding.root) {

        val listener = OnLivePlayViewAlphaImpl(binding.videoCard, true)
    }
}

interface FirstPlayTopStream {

    fun getCurrentPosition(): Int

    fun player(holder: PlayViewHolder, item: DisplayUserBean)

}